package com.gitee.qdbp.general.system.biz.permission.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.jdbc.paging.Paging;
import com.gitee.qdbp.general.system.api.permission.basic.IRoleCoreBasic;
import com.gitee.qdbp.general.system.api.permission.basic.IRoleResourceRefBasic;
import com.gitee.qdbp.general.system.api.permission.basic.IUserRoleRefBasic;
import com.gitee.qdbp.general.system.api.permission.model.RoleCoreBean;
import com.gitee.qdbp.general.system.api.permission.model.RoleCoreWhere;
import com.gitee.qdbp.general.system.api.permission.model.RoleResourceRefBean;
import com.gitee.qdbp.general.system.api.permission.model.RoleResourceRefWhere;
import com.gitee.qdbp.general.system.api.permission.model.UserRoleRefBean;
import com.gitee.qdbp.general.system.api.permission.model.UserRoleRefWhere;
import com.gitee.qdbp.general.system.api.permission.service.IRoleCoreQueryer;

/**
 * 角色信息业务处理类
 *
 * @author zhh
 * @version 170215
 */
@Service
@Transactional(readOnly = true)
public class RoleCoreQueryer implements IRoleCoreQueryer {

    /** 角色信息DAO **/
    @Autowired
    private IRoleCoreBasic roleCoreBasic;

    /** 用户角色关联DAO **/
    @Autowired
    private IUserRoleRefBasic userRoleRefBasic;

    /** 角色权限关联DAO **/
    @Autowired
    private IRoleResourceRefBasic roleResourceRefBasic;

    @Override
    public RoleCoreBean find(String id) throws ServiceException {
        return roleCoreBasic.findById(id);
    }

    @Override
    public RoleCoreBean find(RoleCoreWhere where) throws ServiceException {
        return roleCoreBasic.find(where);
    }

    @Override
    public PageList<RoleCoreBean> list(RoleCoreWhere where, OrderPaging paging) throws ServiceException {
        return roleCoreBasic.list(where, paging);
    }

    @Override
    public PageList<RoleResourceRefBean> listRoleResource(RoleResourceRefWhere where, OrderPaging paging)
            throws ServiceException {
        return roleResourceRefBasic.list(where, paging);
    }

    @Override
    public List<RoleCoreBean> listByUserId(String userId) throws ServiceException {
        UserRoleRefWhere where = new UserRoleRefWhere();
        where.setUserId(userId);
        PageList<UserRoleRefBean> refList = userRoleRefBasic.list(where, OrderPaging.NONE);
        if (refList.isEmpty()) {
            return new ArrayList<>();
        }

        // 查询角色列表
        return listByRefList(refList);
    }

    @Override
    public Map<String, List<RoleCoreBean>> listByUserIds(List<String> userIds) throws ServiceException {
        UserRoleRefWhere where = new UserRoleRefWhere();
        where.setUserIds(userIds);
        PageList<UserRoleRefBean> refList = userRoleRefBasic.list(where, OrderPaging.NONE);
        if (refList.isEmpty()) {
            return new HashMap<>();
        }

        // 查询角色列表
        List<RoleCoreBean> roles = listByRefList(refList);

        Map<String, List<RoleCoreBean>> map = new HashMap<>();
        for (RoleCoreBean role : roles) { // 保证顺序
            for (UserRoleRefBean ref : refList) {
                if (ref.getRoleId().equals(role.getId())) {
                    if (!map.containsKey(ref.getUserId())) {
                        map.put(ref.getUserId(), new ArrayList<RoleCoreBean>());
                    }
                    map.get(ref.getUserId()).add(role);
                }
            }
        }
        return map;
    }

    private List<RoleCoreBean> listByRefList(PageList<UserRoleRefBean> refList) throws ServiceException {

        Set<String> roleIds = new HashSet<>();
        for (UserRoleRefBean bean : refList) {
            roleIds.add(bean.getRoleId());
        }
        // 查询角色列表
        RoleCoreWhere rcw = new RoleCoreWhere();
        rcw.setIds(new ArrayList<>(roleIds));
        OrderPaging p = OrderPaging.of(Paging.NONE, "sortIndex asc, createTime desc");
        return roleCoreBasic.list(rcw, p).toList();
    }
}
